﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Xml;

namespace DiskGarden
{
	[Serializable]
	public class FileNode : Node
	{

		// Attributes - atrybuty pliku takie jak: Folder, Archiwum, Tylko do odczytu, Systemowy
		[XmlAttribute("attributes")]
		public FileAttributes Attributes;

		public VolumeNode GetVolume()
		{
			if (this.type == NodeType.Volume) return this as VolumeNode;
			Node n = this.Parent;
			while (n.Type != NodeType.Volume)
			{
				n = n.Parent;
			}
			return n as VolumeNode;
		}

		public FileNode()
		{
			this.type = NodeType.File;
		}

		internal virtual void WriteXml(XmlWriter xml)
		{
			base.WriteXmlAttributes(xml);
			xml.WriteAttributeString("attr", ((int)Attributes).ToString());
			base.WriteXmlTags(xml);
		}

		internal virtual void ReadXml(XmlReader xml)
		{
			while (xml.MoveToNextAttribute())
			{
				switch (xml.Name)
				{
					case "name": name = xml.Value; break;
					case "path": Path = xml.Value; break;
					case "size": Size = xml.ReadContentAsLong(); break;
					case "date": Date = xml.ReadContentAsDateTime(); break;
					case "attr": Attributes = (FileAttributes)int.Parse(xml.Value); break;
					case "comment": Comment = xml.Value; break;
				}
			}
			xml.MoveToElement();
			if (xml.IsEmptyElement) return;

			if (this.type == NodeType.File)
			{
				xml.Read();
				if (xml.Name == "tags")
				{
					ReadXmlTags(xml);
					xml.ReadEndElement();
				}
			}
		}
	}
}
